package rpc

import (
	"gitee.com/ameise84/e3dds/internal/log"
	net "gitee.com/ameise84/e3net"
)

var (
	_srvHandler = &serverHandler{}
	_agtHandler = &agentHandler{}
)

type serverHandler struct {
}

func (ts *serverHandler) OnNewConnect(tag net.Tag, conn net.Conn) net.ConnContext {
	rc := takeRawContext()
	rc.onInit(conn)
	return rc
}

func (ts *serverHandler) OnClosedConnect(ctx net.ConnContext) {
	rc := ctx.(*rawContext)
	rc.onClosed()
	freeRawContext(rc)
}

type agentHandler struct {
}

func (ts *agentHandler) OnAgentConnect(agent net.TcpAgent, ctx net.ConnContext, conn net.Conn) {
	rc := ctx.(*rawContext)
	rc.onInit(conn)
}

func (ts *agentHandler) OnAgentConnectFailed(agent net.TcpAgent, tag net.Tag, ctx net.ConnContext, err error) {
	log.GetLogger().Error("rpc agent.connect failed").Err(err).Str("tag", tag).Println()
}

func (ts *agentHandler) OnAgentClosedConn(agent net.TcpAgent, tag net.Tag, ctx net.ConnContext) {
	rc := ctx.(*rawContext)
	rc.onClosed()
}
